package com.demo.camel03.prodandconsume;

import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.PropertyConfigurator;

public class ProdAndConsumeDemo {

    public static void main(String[] args) throws Exception {

        // 日志
        PropertyConfigurator.configure("src/conf/log4j.properties");
        PropertyConfigurator.configureAndWatch("src/conf/log4j.properties", 1000);

        CamelContext context = new DefaultCamelContext();
        context.start();


        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                from("direct:start").process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        String message = exchange.getIn().getBody(String.class);
                        message += "!!~";
                        exchange.getOut().setBody(message);
                    }
                }).to("seda:end");

            }
        });


        ProducerTemplate producerTemplate = context.createProducerTemplate();
        producerTemplate.sendBody("direct:start", "hello camel");

        ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
        String message = consumerTemplate.receiveBody("seda:end", String.class);

        System.out.println(message);

        // 通用没有具体业务意义的代码，只是为了保证主线程不退出
        synchronized (ProdAndConsumeDemo.class) {
            ProdAndConsumeDemo.class.wait();
        }
    }
}
